package entity;

import top.SimplePrimaryKey;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

public class User implements SimplePrimaryKey, Comparable<User> {
    private Long    id;
    private String  name;
    private String  nick;
    private String  passwordHash;
    private String  headPic;

    private Integer win;
    private Integer lost;
    private Integer run;
    private Long    score;

    private Integer joinRank;

    private String  state;

    public User() {
        this(null, null, null, null, null, null, null, null, null, null, null);
    }

    public User(
            Long id, String name, String nick, String passwordHash,
            String headPic, Integer win, Integer lost, Integer run,
            Long score, Integer joinRank, String state
    ) {
        this.id           = id;
        this.name         = name;
        this.nick         = nick;
        this.passwordHash = passwordHash;
        this.headPic      = headPic;
        this.win          = win;
        this.lost         = lost;
        this.run          = run;
        this.score        = score;
        this.joinRank     = joinRank;
        this.state        = state;
    }

    public User(ResultSet rs) throws SQLException {
        this.id           = rs.getLong(1);
        this.name         = rs.getString(2);
        this.nick         = rs.getString(3);
        this.passwordHash = rs.getString(4);
        this.headPic      = rs.getString(5);
        this.win          = rs.getInt(6);
        this.lost         = rs.getInt(7);
        this.run          = rs.getInt(8);
        this.score        = rs.getLong(9);
        this.joinRank     = rs.getInt(10);
        this.state        = rs.getString(11);
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", nick='" + nick + '\'' +
                ", passwordHash='null'" +
                ", headPic='" + headPic + '\'' +
                ", win=" + win +
                ", lost=" + lost +
                ", run=" + run +
                ", score=" + score +
                ", joinRank=" + joinRank +
                ", state='" + state + '\'' +
                '}';
    }

    public String toSerializableJson() {
        return "{" +
                "\"id\":" + id +
                ",\"name\":\"" + name + '\"' +
                ",\"nick\":\"" + nick + '\"' +
                ",\"passwordHash\":\"null\"" +
                ",\"headPic\":\"" + headPic + '\"' +
                ",\"win\":" + win +
                ",\"lost\":" + lost +
                ",\"run\":" + run +
                ",\"score\":" + score +
                ",\"joinRank\":" + joinRank +
                ",\"state\":\"" + state + '\"' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(id, user.id) || Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

    public final Long getId() {
        return id;
    }

    public final void setId(Long id) {
        this.id = id;
    }

    public final String getName() {
        return name;
    }

    public final void setName(String name) {
        this.name = name;
    }

    public final String getNick() {
        return nick;
    }

    public final void setNick(String nick) {
        this.nick = nick;
    }

    public final String getPasswordHash() {
        return passwordHash;
    }

    public final void setPasswordHash(String passwordHash) {
        this.passwordHash = passwordHash;
    }

    public final String getHeadPic() {
        return headPic;
    }

    public final void setHeadPic(String headPic) {
        this.headPic = headPic;
    }

    public final Integer getWin() {
        return win;
    }

    public final void setWin(Integer win) {
        this.win = win;
    }

    public final Integer getLost() {
        return lost;
    }

    public final void setLost(Integer lost) {
        this.lost = lost;
    }

    public final Integer getRun() {
        return run;
    }

    public final void setRun(Integer run) {
        this.run = run;
    }

    public final Long getScore() {
        return score;
    }

    public final void setScore(Long score) {
        this.score = score;
    }

    public final Integer getJoinRank() {return joinRank;}

    public final void setJoinRank(Integer joinRank) {this.joinRank = joinRank;}

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @Override
    public Object getPrimaryKey() {
        return this.id;
    }

    @Override
    /**
     * Usage: Only for 排行榜中的TreeSet
     */
    public int compareTo(User o) {
        return (int)(id - o.id);
    }
}
